import plugin from '../../../lib/plugins/plugin.js';
import common from '../../../lib/common/common.js';
import set from '../utils/setting.js';

var default_n = set.getConfig('config')?.default_n;
//定义勿修改
var msg = ''

export class voicecreate extends plugin {
	constructor() {
		super({
			name: '[Xs插件]点歌',
			dsc: '网易云音乐点歌',
			event: 'message',
			priority: 9,
			rule: [
			{
				reg: '^#?xs点歌(.*)$',
				fnc: `dg`
			},
			{
				reg: '^#?xs(歌单(.*)|听\\d+)$',
				fnc: `gd`
			},
			{
				reg: '^#?xs点歌帮助$',
				fnc: `dgnz`
			}
			]
		})
	}

	async dg (e) {
		if (/#?xs点歌/.test(e.msg)) {
	   	  e.msg = e.msg.replace(/#?xs点歌/g, '');
	   	  let msg1 = e.msg;
	   		if (!msg1) {
		   		e.reply(`未知命令格式,请发送【#xs点歌帮助】查看点歌帮助`,true);
		   		return true;
	   		} else {
				let geturl = ''
   				if (!default_n) {
	   				geturl = `https://api.lolimi.cn/API/wydg/api.php?msg=${msg1}&n=1`;
				} else {
	   				geturl = `http://api.lolimi.cn/API/wydg/api.php?msg=${msg1}&n=${default_n}`;
      			}
	   		let res = await fetch(geturl);
	   		res = await res.json();
	   		if (!res.data) {
			e.reply(`返回值错误(可能是api问题)，若确保您的参数无错，然后请联系插件作者反馈`);
				return true;
	   		}
	   		  e.reply(`获取成功\n音乐名:` + res.data.Music + `\n歌手:` + res.data.Singer + `\n音乐链接:` + res.data.Music_Url + `\n正在发送语音,请稍后...:`,true);
	   		  let music = res.data.Url
	   		  let resurl = await uploadRecord(music,0,false);
	   		  e.reply(resurl);
	   		  return true;
	   		}
	 	}
  	}

  	async gd(e) {
		if (/#?xs歌单/.test(e.msg)) {
			e.msg = e.msg.replace(/#?xs歌单/g, '');
	   		msg = e.msg;
	   		if (!msg) {
		   		e.reply(`未知命令格式,请发送【#xs点歌帮助】查看点歌帮助`,true);
		   		return true;
	   		} else {
	   		let geturl = `https://api.lolimi.cn/API/wydg/api.php?msg=${msg}`;
	   		let res = await fetch(geturl);
	   		res = await res.json();
	   		if (!res.data) {
				e.reply(`返回值错误(可能是api问题)，若确保您的参数无错，然后请联系插件作者反馈`);
				return true;
	   		}
	   		let list = res.Msg
	   		let Msg = []
	   		for (let i in list) {
		 		let num = Number(i) + 1
		 		Msg.push(`${num}.${list[i]}`)
   	   		}
	   		let titles = `发送【#xs听+序号】收听对应歌曲`
	   		let title = `点击查看歌单`
	   		Msg = await common.makeForwardMsg(this.e,[titles,Msg.join("\n\n")], title)
	   		e.reply(Msg);
	   		}
	 	}
		if (/#?xs听/.test(e.msg)) {
			e.msg = e.msg.replace(/#?xs听/g, '');
			let xh = e.msg;
			if (!e.msg) {
				e.reply(`未知命令格式,请发送【#xs点歌帮助】查看点歌帮助`,true);
				return true;
			} else {
			let geturl = `https://api.lolimi.cn/API/wydg/api.php?msg=${msg}&n=${xh}`;
			let res = await fetch(geturl);
			res = await res.json();
			if (!res.data) {
		 		e.reply(`歌名获取超时,请重新发送#xs歌单+歌名`);
		 		return true;
			}
			if (res.text =="歌曲列表获取成功") {
		  		e.reply(`错误序号`);
		  		return true;
	    	}
			logger.info('[Xs-plugin|用户命令]#听',msg,xh)
			e.reply(`获取成功\n音乐名:` + res.data.Music + `\n歌手:` + res.data.Singer + `\n音乐链接:` + res.data.Music_Url + `\n正在发送语音,请稍后...`,true);
			let music = res.data.Url
			let resurl = await uploadRecord(music,0,false);
			e.reply(resurl);
	  		}
    	}
  	}

	async dgbz(e) {
		e.reply('可使用命令【#xs点歌+歌名】直接点歌(点歌序号为默认)\n也可使用命令【#xs歌单+歌名】查看歌单列表(仅展示10个搜索结果)\n然后发送【#xs听+序号】进行点歌',true)
		return true;
	}
}